﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Data.Model;
using NHibernate;
using Senparc.Weixin.MP.Entities;
using NHibernate.Criterion;
using System.Collections.Generic;
using ZLCMS.Business.Util;
using Senparc.Weixin.MP.Helpers;
using DotNet.Common.NHibernateUtil;
using Newtonsoft.Json;
using ZLCMS.Business.WeixinService.Data;

namespace ZLCMS.Web.SystemManage.Weixin
{
    public partial class DealApplyLog : System.Web.UI.Page
    {
        protected FrameHelper _frameHelper;

        protected string _userAccount;

        protected WxApplyLog _applyLogDb;
        protected WxApplyConfig _applyDb;
        protected List<WxApplyConfigItem> _itemList;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();

            _frameHelper = new FrameHelper(this);

            //初始化全局变量值
            InitializeGlobalValue();
            //初始化控件值
            InitializeContent();

            if (IsPostBack)
            {
                Save();
            }
        }

        #region 保存操作 更新 +private void Save()
        /// <summary>
        /// 保存操作 新建/更新
        /// </summary>
        private void Save()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                try
                {
                    _applyLogDb.Status = ddl_status.SelectedValue;
                    _applyLogDb.Remark = tb_remark.Text;

                    session.Update(_applyLogDb);
                    session.Flush();
                }
                catch (System.Exception ex)
                {

                    _frameHelper.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }

            _frameHelper.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                navTabId = _frameHelper.NavTabId
            });
        }
        #endregion

        #region 初始化全局变量 + protected void InitializeGlobalValue()
        /// <summary>
        /// 初始化全局变量
        /// </summary>
        protected void InitializeGlobalValue()
        {
            string uid;

            if (!IsPostBack)
            {
                ParamKit.Bind("YesNo_10", ddl_status, null, null, null, "-1", "-1");

                ViewState["uid"] = Request["uid"];
            }

            uid = (string)ViewState["uid"];

            if (!string.IsNullOrEmpty(uid))
            {
                using (ISession session = SessionFactoryHelper.CreateSession())
                {
                    try
                    {
                        _applyLogDb = session.CreateCriteria<WxApplyLog>().Add(Expression.Eq("Id", int.Parse(uid))).Add(Expression.Eq("OwnerAccount", _userAccount)).UniqueResult<WxApplyLog>();

                        if (_applyLogDb == null)
                        {
                            throw new Exception("找不到对应记录.");
                        }

                        _applyDb = session.CreateCriteria<WxApplyConfig>().Add(Expression.Eq("Id", _applyLogDb.ApplyId)).Add(Expression.Eq("OwnerAccount", _userAccount)).UniqueResult<WxApplyConfig>();

                        if (_applyDb == null)
                        {
                            throw new Exception("找不到对应记录.");
                        }

                        _itemList = new List<WxApplyConfigItem>();                        
                        _itemList.Add(JsonConvert.DeserializeObject<WxApplyConfigItem>(_applyDb.ApplyItem1 == null ? "" : _applyDb.ApplyItem1));
                        _itemList.Add(JsonConvert.DeserializeObject<WxApplyConfigItem>(_applyDb.ApplyItem2 == null ? "" : _applyDb.ApplyItem2));
                        _itemList.Add(JsonConvert.DeserializeObject<WxApplyConfigItem>(_applyDb.ApplyItem3 == null ? "" : _applyDb.ApplyItem3));
                        _itemList.Add(JsonConvert.DeserializeObject<WxApplyConfigItem>(_applyDb.ApplyItem4 == null ? "" : _applyDb.ApplyItem4));
                        _itemList.Add(JsonConvert.DeserializeObject<WxApplyConfigItem>(_applyDb.ApplyItem5 == null ? "" : _applyDb.ApplyItem5));
                        _itemList.Add(JsonConvert.DeserializeObject<WxApplyConfigItem>(_applyDb.ApplyItem6 == null ? "" : _applyDb.ApplyItem6));
                        _itemList.Add(JsonConvert.DeserializeObject<WxApplyConfigItem>(_applyDb.ApplyItem7 == null ? "" : _applyDb.ApplyItem7));
                        _itemList.Add(JsonConvert.DeserializeObject<WxApplyConfigItem>(_applyDb.ApplyItem8 == null ? "" : _applyDb.ApplyItem8));
                        _itemList.Add(JsonConvert.DeserializeObject<WxApplyConfigItem>(_applyDb.ApplyItem9 == null ? "" : _applyDb.ApplyItem9));
                        _itemList.Add(JsonConvert.DeserializeObject<WxApplyConfigItem>(_applyDb.ApplyItem10 == null ? "" : _applyDb.ApplyItem10));
                    }
                    catch (System.Exception ex)
                    {
                        _frameHelper.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                        return;
                    }
                }
            }
        }
        #endregion

        #region 填充页面内容
        /// <summary>
        /// 填充页面内容
        /// </summary>
        protected void InitializeContent()
        {
            //页面控件值
            if (!IsPostBack)
            {
                ddl_status.SelectedValue = _applyLogDb.Status;
                tb_remark.Text = _applyLogDb.Remark;
            }
        }
        #endregion
    }
}
